﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using FilStartUps.FILAPI;
using System.Web.UI.WebControls;

namespace FilStartUps.Controls
{
    public partial class FilStartUps_PostArchieves : System.Web.UI.UserControl
    {
        //GetData
        public void GetPostArhieves(int catidFK)
        {
            try
            {
                var postarhieves = FilStartUpsAPI.GetArticleArchieves(catidFK);
                ViewState["Post"] = postarhieves;
                BindDataList(Convert.ToInt32(ViewState["Skip"]), catidFK);
                dlFilStartUps_PostArchieves.Visible = true;

                if (postarhieves.Count > 5)
                {
                    lnkbNext.Visible = true;
                    lnkbPrev.Visible = true;
                    lnkbPrev.Enabled = false;
                }
                else
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                }
                
            }
            catch (Exception)
            {

            }
        }
        public string ConvertDate(string date)
        {
            string date_;
            try
            {
                DateTime _date = Convert.ToDateTime(date);
                date_ = _date.ToShortDateString();//String.Format("{0:d/M/yyyy}", date);
            }
            catch
            {
                date_ = date.ToString();
            }
            return date_;
        }

        //Pagination
        private void BindDataList(int Skip, int catid)
        {
            int iPage = Skip + 5;
            int currPage = iPage / 5;
            double TotalPage;

            var postarhieves = FilStartUpsAPI.GetArticleArchieves(catid);


            TotalPage = Math.Ceiling((double)postarhieves.Count / 5);

            var postarhieves_ = postarhieves.Skip(Skip).Take(5);

            if (iPage > postarhieves.Count())
            {
                lnkbNext.Enabled = false;
            }
            else
            {
                lnkbNext.Enabled = true;
            }

            dlFilStartUps_PostArchieves.DataSource = postarhieves_;
            dlFilStartUps_PostArchieves.DataBind();

            litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Skip"] = 0;

                int catid = Convert.ToInt32(Request.QueryString["c"].Trim());
                GetPostArhieves(catid);
            }
        }

        //Tuncate Description
        public string Truncate(string Description)
        {
            string desc;

            try
            {
                desc = Description.ToString().Substring(0, 100) + "...";
            }
            catch (Exception)
            {
                desc = Description;
            }
            return desc;
        }

        protected void dlFilStartUps_PostArchieves_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                Label lbpostidPK = (Label)dlFilStartUps_PostArchieves.Items[e.Item.ItemIndex].FindControl("postidPK");
                LinkButton lnkpostdesc = (LinkButton)dlFilStartUps_PostArchieves.Items[e.Item.ItemIndex].FindControl("posttitle");
              
                //Redirect Content.aspx?
                //Archieves to SingleOrDefault();
                Response.Redirect("~/Article.aspx?p=" + lbpostidPK.Text);
            }
        }

        protected void lnkbNext_Click(object sender, EventArgs e)
        {
            lnkbPrev.Enabled = true;
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 5;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["c"].Trim()));
        }

        protected void lnkbPrev_Click(object sender, EventArgs e)
        {
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 5;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["c"].Trim()));
            if (Convert.ToInt32(ViewState["Skip"]) == 0)
            {
                lnkbPrev.Enabled = false;
            }
        }        
    }
}